package com.ardf.service.impl;

import com.ardf.common.EntityConstants;
import com.ardf.entity.Algorithm;
import com.ardf.entity.Grade;
import com.ardf.entity.Project;
import com.ardf.entity.dto.GradeTableDto;
import com.ardf.entity.dto.StartGamePersonTableDto;
import com.ardf.entity.dto.StartGameRelayTableDto;
import com.ardf.mapper.AlgorithmMapper;
import com.ardf.mapper.GradeMapper;
import com.ardf.mapper.ProjectMapper;
import com.ardf.service.IGradeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ShiYunHao
 * @since 2025-01-06
 */
@Service
public class GradeServiceImpl extends ServiceImpl<GradeMapper, Grade> implements IGradeService {
    private final GradeMapper gradeMapper;
    private final ProjectMapper projectMapper;
    private final AlgorithmMapper algorithmMapper;

    public GradeServiceImpl(GradeMapper gradeMapper, ProjectMapper projectMapper, AlgorithmMapper algorithmMapper) {
        this.gradeMapper = gradeMapper;
        this.projectMapper = projectMapper;
        this.algorithmMapper = algorithmMapper;
    }

    @Override
    public Page<GradeTableDto> searchAll(Integer pageSize, Integer pageNum, Long competitionId, Long projectId, Boolean isLastResultDecided) {
        Page<GradeTableDto> page = new Page<>(pageNum, pageSize);
        Page<GradeTableDto> result = gradeMapper.searchAll(page, competitionId, projectId, isLastResultDecided);
        for (GradeTableDto record : result.getRecords()) {
            record.setProjectName(getProjectNameByProjectId(record.getProjectId()));
            if (record.getIsLastResultDecided()) {
                Algorithm algorithm = algorithmMapper.selectOne(new LambdaQueryWrapper<Algorithm>()
                        .eq(Algorithm::getRuleId, record.getRuleId())
                        .in(Algorithm::getStatus, List.of(EntityConstants.ALGORITHM_STATUS_AGREED_FINAL,
                                EntityConstants.ALGORITHM_STATUS_NO_AGREED_FINAL)));
                record.setLastAlgorithmName(getAlgorithmTypeNameByType(algorithm.getType()));
            }
        }
        return result;
    }

    @Override
    public Page<StartGamePersonTableDto> startGameSearchAllPerson(Integer pageSize, Integer pageNum, Long projectId, Boolean isTerminal, Long groupId) {
        Page<StartGamePersonTableDto> page = new Page<>(pageNum, pageSize);
        Page<StartGamePersonTableDto> pages = gradeMapper.startGameSearchAllPerson(page, projectId, groupId, isTerminal);
        for (StartGamePersonTableDto dto:pages.getRecords()){
            System.out.println(dto);
        }
        return pages;
    }

    @Override
    public Page<StartGameRelayTableDto> startGameSearchAllRelay(Integer pageSize, Integer pageNum, Long projectId, Boolean isTerminal, Long groupId) {
        Page<StartGameRelayTableDto> page = new Page<>(pageNum, pageSize);
        return gradeMapper.startGameSearchAllRelay(page,projectId,groupId,isTerminal);
    }

    /**
     * 通过id获得项目名
     */
    private String getProjectNameByProjectId(Long projectId) {
        Map<Byte, String> projectType = Map.of(EntityConstants.PROJECT_TYPE_FAST, "快速",
                EntityConstants.PROJECT_TYPE_MEDIUM, "中距离",
                EntityConstants.PROJECT_TYPE_SHORT, "短距离",
                EntityConstants.PROJECT_TYPE_SUNLIGHT, "阳光测向",
                EntityConstants.PROJECT_TYPE_STANDARD, "标准距离");
        Map<Byte, String> projectFrequency = Map.of(EntityConstants.PROJECT_FREQUENCY_2m, "2m",
                EntityConstants.PROJECT_FREQUENCY_80m, "80m");
        Map<Byte, String> projectCategory = Map.of(EntityConstants.PROJECT_CATEGORY_PERSON, "个人",
                EntityConstants.PROJECT_CATEGORY_RELAY, "接力");
        Project project = projectMapper.selectById(projectId);
        return projectType.get(project.getType()) + "-" + projectFrequency.get(project.getFrequency()) + "-" +
                projectCategory.get(project.getCategory());
    }

    /**
     * 根据数据库字段转译成中文
     */
    private String getAlgorithmTypeNameByType(Byte type) {
        if (type != null) {
            Map<Byte, String> algorithmTypeMap = Map.of(EntityConstants.ALGORITHM_TYPE_NO_ORDER, "无顺序",
                    EntityConstants.ALGORITHM_TYPE_ORDER_NO_BACK_FIRST, "有顺序，不能反悔，取第一个站开始算的最长有效子路线",
                    EntityConstants.ALGORITHM_TYPE_ORDER_BACK_EFFECTIVE, "有顺序，能反悔，取最长有效子路线",
                    EntityConstants.ALGORITHM_TYPE_ORDER_NO_BACK_EFFECTIVE, "有顺序，不能反悔，取最长有效子路线",
                    EntityConstants.ALGORITHM_TYPE_RELAY_NO_ORDER, "接力赛无顺序",
                    EntityConstants.ALGORITHM_TYPE_FAST_NO_ORDER, "快速赛无顺序",
                    EntityConstants.ALGORITHM_TYPE_SUNLIGHT_ORDER, "阳光严格有顺序",
                    EntityConstants.ALGORITHM_TYPE_SUNLIGHT_RELAY_ORDER, "阳光接力严格有顺序");
            return algorithmTypeMap.get(type);
        } else {
            return null;
        }
    }

    public  Grade selectByProIdAndAthId(Long projectId,Long athleteId){
        return gradeMapper.selectGradeByProIdAndAthId(projectId,athleteId);
    }

    public  Grade selectByProIdAndTId(Long projectId,Long teamId){
        return gradeMapper.selectGradeByProIdAndTId(projectId,teamId);
    }

    @Override
    public boolean updateById(Grade grade) {
        int i = gradeMapper.updateById(grade);
        return true;
    }

    @Override
    public StartGamePersonTableDto startGameSearchOnlyPerson(Long gradeId) {
       return gradeMapper.startGameSearchOnlyPerson(gradeId);
    }

    @Override
    public StartGameRelayTableDto startGameSearchOnlyTeam(Long gradeId) {
        return gradeMapper.startGameSearchOnlyTeam(gradeId);
    }

}
